package com.olah.ws.controller;


import com.olah.common.entity.dto.UtilResponse;
import com.olah.common.entity.po.ws.dto.chat.PrivateMsg;
import com.olah.common.entity.po.ws.dto.core.WebSocketRequest;
import com.olah.ws.service.PrivateMsgService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.olah.ws.service.WebSocketService.notifyPrivateMsg;

/**
 * Private Message Controller
 */
@RestController
@RequestMapping("/ws/private")
public class PrivateMsgController {

    @Resource
    private PrivateMsgService service;

    /**
     * 读取指定用户的所有私信
     * @param id 用户 id
     * @param unreadOnly 是否只查询未阅读的
     */
    @GetMapping("/readAll")
    public UtilResponse<List<PrivateMsg>> readAll(
            @RequestParam("id") int id,
            @RequestParam(value = "unreadOnly", required = false) boolean unreadOnly
    ) {
        return UtilResponse.success(service.readAll(id, unreadOnly));
    }

    /**
     * 执行系统消息的已阅读
     * @param chatId 消息 id
     */
    @GetMapping("/readed")
    public UtilResponse<Boolean> readed(@RequestParam("id") String chatId) {
        service.doRead(chatId);
        return UtilResponse.success(true);
    }

    /**
     * 发送一条私信给指定用户
     * @param msg 请求内容
     */
    @PostMapping("/sendToUser")
    public UtilResponse<Boolean> sendToUser(@RequestBody WebSocketRequest<PrivateMsg> msg) {
        service.sendToUser(msg.getData());
        notifyPrivateMsg(msg.getReceiver().getId(), "有人给你发了一条消息", msg.getData(), "receiveMsgFromUser");
        return UtilResponse.success(true);
    }

}
